package com.redis.controller;

import org.redisson.api.*;
import org.redisson.client.RedisClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletionStage;

/***
 * 程序接口调用方式
 * 异步执行方式
 */
@RestController
public class AtomicController {

    @Resource
    private RedissonClient redissonClient;

    @RequestMapping(value = "/atomic")
    public void atomic(){

        RAtomicDouble dobbo = redissonClient.getAtomicDouble("dobbo");


        RAtomicLong aLong = redissonClient.getAtomicLong("long");
        aLong.set(3);
        // 同步执行方式
        boolean b = aLong.compareAndSet(3, 401);
        // 异步执行方式
        RFuture<Boolean> booleanRFuture = aLong.compareAndSetAsync(3, 401);
        long l = aLong.get();

        System.out.println(l);
        // JDK 1.8+ 适用
        booleanRFuture.whenComplete((res, exception) -> {
            // ...
            boolean b1 = res.booleanValue();
            System.out.println(b);
            String message = exception.getMessage();
            System.out.println(message);
        });
        // 或者
        CompletionStage<Void> exceptionally = booleanRFuture.thenAccept(res -> {
            // 处理返回
        }).exceptionally(exception -> {
            // 处理错误
            return null;
        });


        RDoubleAdder doubleaddr = redissonClient.getDoubleAdder("doubleaddr");


        redissonClient.getLongAdder("");

    }

}
